package com.zhentao.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.ChargeStationCommentMapper;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.pojo.core.ChargeStationComment;
import com.zhentao.model.pojo.core.dto.ChargeStationInfoDto;
import com.zhentao.model.pojo.user.ChargeUser;
import com.zhentao.service.ChargeStationCommentService;
import com.zhentao.user.ChargeUserFeignApi;
import com.zhentao.user.ChargeUserThreadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.zhentao.common.user.UserCommon.CHARGE_USER_LOGIN_KEY_SCHEDULE;

/**
 * @author 22115
 * @description 针对表【charge_station_comment】的数据库操作Service实现
 * @createDate 2024-12-25 15:47:37
 */
@Service
public class ChargeStationCommentServiceImpl extends ServiceImpl<ChargeStationCommentMapper, ChargeStationComment>
        implements ChargeStationCommentService {


    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public ResponseResult getCoreCommentList(ChargeStationInfoDto chargeStationInfoDto) {
        if (chargeStationInfoDto == null) {
            return ResponseResult.errorResult(500, "不能为空");
        }

        List<ChargeStationComment> list = this.list(Wrappers.<ChargeStationComment>lambdaQuery().eq(ChargeStationComment::getStationId, chargeStationInfoDto.getId()));
        return ResponseResult.okResult(list);

    }

    @Resource
    ChargeUserFeignApi chargeUserFeignApi;

    @Override
    public ResponseResult addCoreComment(ChargeStationComment chargeStationComment) {
        if (!StringUtils.isNotBlank(chargeStationComment.getContent()) && !StringUtils.isNotBlank(chargeStationComment.getStationId().toString())) {
            return ResponseResult.errorResult(500, "参数不能为空");
        }

//        ChargeUser chargeUserThreadLocal = ChargeUserThreadUtils.getChargeUserThreadLocal();
        String userId = redisTemplate.opsForValue().get(CHARGE_USER_LOGIN_KEY_SCHEDULE);
        if(userId == null) return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        chargeStationComment.setUserId(Integer.valueOf(userId));
        ResponseResult userPoint = chargeUserFeignApi.getUserPoint(Long.valueOf(userId));

        System.err.println(userPoint.getData());

        ChargeUser chargeUser = JSON.parseObject(JSON.toJSONString(userPoint.getData()), ChargeUser.class);


//

        chargeStationComment.setAcatarImg(chargeUser.getAvatarImg());
        chargeStationComment.setUserName(chargeUser.getNickName());
        this.save(chargeStationComment);
        return ResponseResult.okResult("添加成功");
    }
}




